package com.example.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo1.dao.ShareDAO;
import com.example.demo1.entity.Project;
import com.example.demo1.entity.Share;
import com.example.demo1.service.ShareService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ShareServiceImpl implements ShareService {
    @Autowired
    ShareDAO shareDao;

    public void addShare(Share share) {
        share.setTime(new Date());
        shareDao.insert(share);
    }
    public IPage<Share> getShareList(int currentPage, int pageSize, Share share){
//        创建分页对象，current为当前页数，size为每天最大记录数
        Page<Share> pageShare=new Page<>(currentPage,pageSize);
        QueryWrapper<Share> wrapperProject=new QueryWrapper<>();
//        第一个参数为是否执行条件，为true则执行该条件
        wrapperProject.like(StringUtils.isNullOrEmpty(share.getNickname()),"nickname",share.getNickname());
//        根据项目id查询
        wrapperProject.eq(share.getId()>0,"id",share.getId());
//        id倒叙
        wrapperProject.orderByDesc("id");
//       调用分页查询方法，传入分页对象--pageShare，wrapperProject是构造条件对象
        return shareDao.selectPage(pageShare,wrapperProject);
    }

    @Override
    public List<Share> getAllShares() {
        QueryWrapper<Share> queryWrapper = new QueryWrapper<>();
        return shareDao.selectList(queryWrapper);
    }
}
